<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-08 21:49:42
 * @Last Modified time: 2023-02-09 15:58:16
 */

namespace app\common\model\store;

use frank\base\BaseModel;
use frank\traits\ModelTrait;
use app\common\enum\EnumFactory;
use app\common\enum\store\DeliveryTypeEnum;

/**
 * 订单发货单
 * Class OrderDeliveryModel
 * @package app\common\model\store
 */
class OrderDeliveryModel extends BaseModel
{
    use ModelTrait;

    /**
     * 数据表主键
     */
    protected $pk = 'id';

    /**
     * 模型名称
     */
    protected $name = 'store_order_delivery';
    

    /**
     * 商品列表
     * @return \think\model\relation\HasMany
     */
    public function goods()
    {
        return $this->hasOne("app\\common\\model\\store\\OrderGoodsModel",'id','order_goods_id')->bind([
        	'goods_name'=>'goods_name',
        	'total_num'=>'total_num'
        ]);
    }

    /**
     * 发货状态修改器
     * @param $value
     * @return array
     */
    public function getDeliveryStatusAttr($value,$data)
    {
        $item = $this->getDeliveryTypeItem($value,$data['delivery_type']);
        return $item;
    }

    public function getDeliveryTypeItem($value,$deliveryType,$status = 'delivery'){
        $delivery = DeliveryTypeEnum::describe();
        return DeliveryTypeEnum::describe()[$deliveryType][$status][$value];
    }

    /**
     * order_id搜索器
     */
    public function searchOrderIdAttr($query, $value)
    {
        if (is_array($value)) {
            $query->whereIn('order_id', $value);
        } else {
            $query->where('order_id', $value);
        }
    }

    /**
     * order_goods_id搜索器
     */
    public function searchOrderGoodsIdAttr($query, $value)
    {
        if (is_array($value)) {
            $query->whereIn('order_goods_id', $value);
        } else {
            $query->where('order_goods_id', $value);
        }
    }

    /**
     * delivery_status搜索器
     */
    public function searchDeliveryStatusAttr($query, $value)
    {
        $query->where('delivery_status', $value);
    }

}